package com.superm.service.impl;

import com.alibaba.fastjson.JSON;
import com.superm.entity.Goods;
import com.superm.entity.RepostoryType;
import com.superm.entity.Stock;
import com.superm.mappers.GoodsMapper;
import com.superm.mappers.RepostoryMapper;
import com.superm.mappers.StockMapper;
import com.superm.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private RepostoryMapper repostoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<Stock> allstock(Stock inv, Integer pageNo, Integer pageSize) {
        return stockMapper.allInventory(inv,(pageNo-1)*pageSize,pageSize);
    }
    /**
     * 实现根据指定商品编号查询的库存信息
     * @param stockId
     * @return
     */
    @Override
    public Stock selectGoodsId(Integer stockId) {
        return stockMapper.selectGoodsId(stockId);
    }


    @Override
    public Integer selectCount(Stock inv) {
        return stockMapper.selectCount(inv);
    }

    /**
     * 实现异步根据仓库编号查询库存信息(未上架)
     * @param rId
     * @return
     */
    @Override
    public Map<String, Object> ajaxStockRepostory(Integer rId) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<Stock> stocks =   stockMapper.selectrIdMationInfo(rId);
        map.put("code",0);
        map.put("data", JSON.toJSONString(stocks));
        return map;
    }

    /**
     * 实现根据异步查询库存信息(已上架)
     * @param rId
     * @return
     */
    @Override
    public Map<String, Object> ajaxStockRepostoryTwo(Integer rId) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<Stock> stocks =   stockMapper.selectrIdMationInfoTwo(rId);
        map.put("code",0);
        map.put("data", JSON.toJSONString(stocks));
        return map;
    }
    /**
     * 实现异步方式查询指定库存信息
     * @param stockId
     * @return
     */
    @Override
    public Map<String, Object> ajaxQueryId(Integer stockId) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",JSON.toJSONString(stockMapper.queryId(stockId)));
        return map;
    }

    /**
     * 实现异步查询指定库存
     * @param stockId
     * @return
     */
    @Override
    public Map<String, Object> ajaxQueryTwo(Integer stockId) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",JSON.toJSONString(stockMapper.queryIdTwo(stockId)));
        return map;
    }

    @Override
    public Map<String, Object> upInventory(Stock stock,Integer capacity) {
        Map<String,Object> map = new HashMap<>();
        Stock Ystock = stockMapper.queryId(stock.getStockId());
        map.put("success",false);
        map.put("msg","修改失败!");
        RepostoryType rep = repostoryMapper.queryRid(stock.getReponseid());
        Integer YCapcity =  Ystock.getStockCount()*capacity;//得到原库存数的占用总容量
        Integer XCapcity = stock.getStockCount()*capacity;//得到新的库存占用量 加上选中仓库的已用容量
        System.out.println("szzzzzzzzzzzzzzzz");
        System.out.println(stock.getStockCount());
        System.out.println(capacity);
        //先减去原仓库所占用消耗容量 YCapcity
        repostoryMapper.XRepcapCityxhao(YCapcity,Ystock.getReponseid());
        System.out.println("sssccccccccccccccccccc");
        System.out.println(YCapcity);
        //1.首先判断选中的仓库是否是原仓库
        if(Ystock.getReponseid()!=stock.getReponseid()){
            //2.不是原仓库,修改分为两个方法
            //在重新加上新的仓库消耗容量 XCapcity
            repostoryMapper.ZRepcapCityxhao(XCapcity,stock.getReponseid());
        }else{
            //1.是原仓库直接修改就行
            //在重新加上新的占用容量 XCapcity
            repostoryMapper.ZRepcapCityxhao(XCapcity,Ystock.getReponseid());
            System.out.println("bbbbbbbbbbbbbbb");
            System.out.println(XCapcity);
        }
        if(stockMapper.upInventory(stock)>0){
            map.put("success",true);
            map.put("msg","修改成功!");
        }
        return map;
    }


    @Override
    public Integer delInventorys(List<String> invs) {
        return stockMapper.delInventorys(invs);
    }

    @Override
    public  Map<String,Object> addInventory(Stock stock){
        Map<String,Object> map = new HashMap<>();

        Goods goods = new Goods();
        goods.setGoodsId(stock.getGoodsid());
        Goods goods1 = goodsMapper.allGoods(goods);
        repostoryMapper.ZRepcapCityxhao(stock.getStockCount()*goods1.getCapacity(),stock.getReponseid());
        if(stockMapper.addInventory(stock)>0){
            map.put("success",true);
            map.put("msg","添加成功");
        }else{
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    @Override
    public Stock queryId(Integer stockId) {
        return stockMapper.queryId(stockId);
    }


    @Override
    public Map<String, Object> upJaoyan(Stock stock,Integer capacity) {
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        map.put("msg","");
        Stock Ystock = stockMapper.queryId(stock.getStockId());
        RepostoryType rep = repostoryMapper.queryRid(stock.getReponseid());
        Integer YstockCount1 = Ystock.getStockCount()*capacity;//得到该商品在原仓库所占用的容量
        Integer YCapcityxhao = rep.getCapcityxhao()-YstockCount1;//拿仓库的已用容量减去商品库存的所占容量  为了去判断 重新输入的
        //库存容量是否大于 该仓库的总容量
        Integer XCapcityxhao = stock.getStockCount()*capacity; //这样得到 新的库存容量乘以 但容量的 所要占用的容量 然后加上YCapcityxhao 进行比较是否大于总容量
        Integer Capcityxhao = YCapcityxhao+XCapcityxhao;
        //1.判断选中的仓库与原仓库是否一致，如果一致
        //1. 获取选中仓库的总容量和已用容量
        //2.算出原有商品库存数量占用容量
        //2.如果不一致  用输入的库存数量 乘以 单商品的占用容量 取得 该商品的总库存的所占容量
        //1.获取选中仓库的已用容量加上 2的总库存所占容量是否大于 选中仓库的总容量 大于 返回false该数量 仓库容不下!  小于返回true
        if(Ystock.getReponseid()==stock.getReponseid()){
            if(Ystock.getStockCount()!=stock.getStockCount()){
                //选择的库存数 和原来的库存数不一致时做的判断
                System.out.println("ssssssssssssssssss");
                System.out.println(Capcityxhao);
                System.out.println(rep.getCapcity());
                if(Capcityxhao>rep.getCapcity()){
                    map.put("success",false);
                    map.put("msg","该数量 仓库容不下!");
                }
            }
        }else{//原仓库和选择的仓库不一致时做的操作
            // 1.查询出来选择的仓库的信息rep
            //2.根据输入的仓库数量进行乘以商品所占容量  算出商品总容量  加上 选择的仓库已用容量  比较  是否大于该仓库的总容量
            Integer XCapcity = stock.getStockCount()*capacity+rep.getCapcityxhao();//得到该商品加上选择仓库的已用容量的总和
            System.out.println("ssssssssssssss");
            System.out.println(XCapcity);
            System.out.println(stock.getStockCount()*capacity);
            System.out.println(rep.getCapcityxhao());
            System.out.println(rep.getCapcity());
            if(XCapcity>rep.getCapcity()){
                map.put("success",false);
                map.put("msg","该数量 仓库容不下!");
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> addJiaoyan(Stock stock) {
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        map.put("msg","");
        Goods goods = new Goods();
        goods.setGoodsId(stock.getGoodsid());
        Goods goods1 = goodsMapper.allGoods(goods);
        RepostoryType rep =  repostoryMapper.queryRid(stock.getReponseid());
        Double XCapacity = goods1.getCapacity()*stock.getStockCount()+rep.getCapcityxhao();//得到商品占用总容量 加上 选择的仓库的已用容量 是否大于仓库的总容量
        if(XCapacity>rep.getCapcity()){
            map.put("success",false);
            map.put("msg","该容量,仓库容不下!");
        }
        return map;
    }
}
